<template>
  <div class="container gray">
    <div class="shop_cart">
      <!-- 全选按钮 -->
      <div class="select_all" @click="changeSelectAll">
          <span class="iconfont" :class="isInCartAll ? 'iconwebicon19' : 'icondanxuanweixuanzhong'"></span>
          全选
        </div>
      <!-- 购买商品列表 -->
      <ul>
          <li class="goods_box" v-for="(item, index) in cartGoods" :key="item.goods_id">
            <!-- 左侧选择按钮 -->
            <div class="iconfont" :class="item.isInCart ? 'iconwebicon19' : 'icondanxuanweixuanzhong'" @click="changeSelect(item.goods_id)"></div>
            <!-- 中间商品信息 -->
            <router-link to="/detail" tag="div" class="goods_img" @click.native="getGoodsById(item.goods_id)">
              <img :src="item.goods_imgs[0]">
            </router-link>
            <div class="goods_info">
              <h3 class="goods_title">{{ item.goods_name}}</h3>
              <div class="goods_price">
                <span class="item_current">￥{{ item.goods_price }}</span>
                <span class="item_oldPrice">{{ item.goods_oldprice | priceFilter }}</span>
              </div>
            </div>
            <!-- 右侧商品数量 -->
            <cart-control :good="item" :id="item.goods_id" :index="index"></cart-control>
          </li>
      </ul>
    </div>
  </div>
</template>

<script>
import CartControl from '@/pages/common/CartControl.vue'
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'CartContent',
  data () {
    return {
      isSelectAll: this.isInCartAll
    }
  },
  computed: {
    ...mapState(['cartGoods']),
    ...mapGetters(['isInCartAll'])
  },
  filters: {
    priceFilter (value) {
      if (value !== 0) return '¥' + value
      return ''
    }
  },
  methods: {
    // 查询商品详情
    getGoodsById (id) {
      this.$store.commit('getGoodsById', id)
    },
    // 切换购物车中全部商品的选择状态
    changeSelectAll () {
      this.$store.commit('changeSelectAll')
    },
    // 切换商品的选择状态
    changeSelect (id) {
      this.$store.commit('changeSelect', id)
    }
  },
  components: {
    CartControl
  },
  mounted () {
    this.isSelectAll = this.isInCartAll
  }
}
</script>

<style lang="less" scoped>
.container {
  overflow: hidden;
  flex: 1;
  width: 15rem;
  padding: 2rem .4rem 4.4rem;
}
.shop_cart {
  width: 100%;
  overflow-x: hidden;
  overflow-y: scroll;
    &::-webkit-scrollbar {
      display: none;
    }
  padding: .4rem;
  border-radius: .4rem;
  background-color: #fff;
  // 全选按钮区域
  .select_all {
    width: 100%;
    line-height: .8rem;
    font-size: .56rem;
    color: rgb(255, 20, 147);
    border-bottom: 1px solid rgba(232, 232, 232, 0.3);
    // 左侧选择按钮
    .iconfont {
      vertical-align: middle;
    }
  }
}
// 左侧选择按钮
.iconfont {
  font-size: .8rem;
}
.icondanxuanweixuanzhong {
  color: #eee;
}
.iconwebicon19 {
  color: rgb(255, 20, 147);
}
// 购买商品列表
.goods_box {
  position: relative;
  display: flex;
  align-items: center;
  height: 3rem;
  padding: .4rem 0;
  // 中间商品信息
  .goods_img {
    width: 3rem;
    height: 3rem;
    margin: 0 .4rem;
    img {
      width: 100%;
    }
  }
  .goods_info {
    overflow: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 100%;
    .goods_title {
      width: 100%;
      font-size: .56rem;
      color: #333;
      // 强制文字换行
      // white-space: pre-wrap;
      // word-wrap: break-word;
    }
    .goods_price {
      width: 100%;
      .item_current {
        font-size: .64rem;
        color: rgb(255, 20, 147);
      }
      .item_oldPrice {
        font-size: .48rem;
        text-decoration: line-through;
        color: #999;
        padding-left: .4rem;
      }
    }
  }
}
</style>
